package com.qf.oop.extend;

import java.util.Random;

/**
 * 动物园有什么？
 */
public class Demo02 {

    public static void main(String[] args) {
        Animal[] zoo = createZoo(10);
        printZoo(zoo);
    }

    /**
     * 打印出来 cat的数量、dog的数量和fish的数量
     * @param zoo
     */
    public static void printZoo(Animal[] zoo){
        int catNum = 0;
        int dogNum =0;
        int fishNum = 0;

        for (int i = 0; i < zoo.length; i++) {
            Animal animal = zoo[i];
            if (animal instanceof Cat){
                catNum++;
            }else if (animal instanceof Dog){
                dogNum++;
            }else {
                fishNum++;
            }
        }
        System.out.println("catNum="+catNum);
        System.out.println("dogNum="+dogNum);
        System.out.println("fishNum="+fishNum);
    }

    /**
     * 创建一个包含了 number数量的 Animal的数组
     * @param number
     * @return
     */
    public static Animal[] createZoo(int number){
        Animal[] animals = new Animal[number];

        Random random = new Random();
        for (int i = 0; i < animals.length; i++) {
            int flag = random.nextInt(3);
            switch (flag){
                case 0:
                    Cat cat = new Cat();
                    animals[i] = cat;
                    break;
                case 1:
                    Dog dog = new Dog();
                    animals[i] =dog;
                    break;
                case 2:
                    Fish fish = new Fish();
                    animals[i] = fish;
                    break;
            }
        }

        return animals;
    }

}
